﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Lifang.Dao;
using System.Collections.Specialized;
using Lifang.Entites;

namespace Lifang.API.Controllers
{
    [Produces("application/json")]
    [Route("api/Common")]
    public class CommonController : BaseController
    {
        public JsonResult Query(string query_key = "SysUserInfoView")
        {
            if (string.IsNullOrWhiteSpace(query_key))
            {
                return Json("query_key不能为空");
            }

            switch (query_key)
            {
                //case "SysUserInfoView":
                //    return Json(SysUserInfoView());
                //case "SysDmlView":
                //    return Json(SysDmlView());
                case "CategoryView":
                    return Json(CategoryView());
                case "ArticleView":
                    return Json(ArticleView());
                case "ArticleContentView":
                    return Json(ArticleContentView());
                case "PageConfigView":
                    return Json(PageConfigView());
                //case "PicView":
                //    return Json(PicView());
                default:
                    return Json("未找到对应的dmlKey");
            }
        }       

        [HttpGet("CategoryView")]
        public ResultListPager<DP_Category> CategoryView()
        {
            var dp = Pager;
            dp.PageSize = 0;
            dp.OrderField = " Id  DESC  ";
            var nvc = new NameValueCollection();
            if (Request.Query != null)
            {
                foreach (var key in Request.Query.Keys)
                {
                    nvc.Add(key, Request.Query[key]);
                }
            }
            var list = new CommonQuery().SelectViewPagerByKey<DP_Category>("CategoryView", nvc, dp);
            var result = new ResultListPager<DP_Category>();
            result.ResultList = list;
            result.ResultPager = dp;
            return result;
        }

        [HttpGet("ArticleView")]
        public ResultListPager<DP_Article> ArticleView()
        {
            var dp = Pager;
            dp.PageSize = 0;
            dp.OrderField = " Id  DESC  ";
            var nvc = new NameValueCollection();
            if (Request.Query != null)
            {
                foreach (var key in Request.Query.Keys)
                {
                    nvc.Add(key, Request.Query[key]);
                }
            }
            var list = new CommonQuery().SelectViewPagerByKey<DP_Article>("ArticleView", nvc, dp);
            var result = new ResultListPager<DP_Article>();
            result.ResultList = list;
            result.ResultPager = dp;
            return result;
        }

        [HttpGet("ArticleContentView")]
        public ResultListPager<DP_ArticleContent> ArticleContentView()
        {
            var dp = Pager;
            dp.PageSize = 0;
            dp.OrderField = " Id  ASC  ";
            var nvc = new NameValueCollection();
            if (Request.Query != null)
            {
                foreach (var key in Request.Query.Keys)
                {
                    nvc.Add(key, Request.Query[key]);
                }
            }
            var list = new CommonQuery().SelectViewPagerByKey<DP_ArticleContent>("ArticleContentView", nvc, dp);
            var result = new ResultListPager<DP_ArticleContent>();
            result.ResultList = list;
            result.ResultPager = dp;
            return result;
        }

        [HttpGet("PageConfigView")]
        public ResultListPager<DP_PageConfig> PageConfigView()
        {
            var dp = Pager;
            dp.PageSize = 0;
            dp.OrderField = " Id  ASC  ";
            var nvc = new NameValueCollection();
            if (Request.Query != null)
            {
                foreach (var key in Request.Query.Keys)
                {
                    nvc.Add(key, Request.Query[key]);
                }
            }
            var list = new CommonQuery().SelectViewPagerByKey<DP_PageConfig>("PageConfigView", nvc, dp);
            var result = new ResultListPager<DP_PageConfig>();
            result.ResultList = list;
            result.ResultPager = dp;
            return result;
        }

        public ResultListPager<DP_Pic> PicView()
        {
            var dp = Pager;
            dp.PageSize = 0;
            dp.OrderField = " Id  ASC  ";
            var nvc = new NameValueCollection();
            if (Request.Query != null)
            {
                foreach (var key in Request.Query.Keys)
                {
                    nvc.Add(key, Request.Query[key]);
                }
            }
            var list = new CommonQuery().SelectViewPagerByKey<DP_Pic>("PicView", nvc, dp);
            var result = new ResultListPager<DP_Pic>();
            result.ResultList = list;
            result.ResultPager = dp;
            return result;
        }
    }
}